package com.shiku.imserver.hander;

import com.shiku.imserver.common.constant.IMLoggers;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.service.IMBeanUtils;
import java.nio.ByteBuffer;
import org.tio.core.ChannelContext;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.Packet;

public abstract class AbstractProtocolHandler extends ImServerHandler {
    public abstract Packet decode(ByteBuffer byteBuffer, ChannelContext channelContext) throws AioDecodeException;

    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        ImPacket impacket = (ImPacket) packet;
        
        if (impacket.getCommand() == 0) {
            IMLoggers.handler.error(" command is 0 ");
        } else {
            IMBeanUtils.getMessageProcess().dispatch(impacket, channelContext);
        }
    }

    public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException {
        return decode(buffer, channelContext);
    }
}
